import cv2 as cv
import numpy as np


def find_contours_demo(image):
    gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
    blured = cv.GaussianBlur(gray,(5,5),0)
    ret , binary = cv.threshold(blured,0,255,cv.THRESH_BINARY|cv.THRESH_OTSU)

    edge = cv.Canny(binary,0,250)
    cv.imshow('edge',edge)

    contours,hirarchy = cv.findContours(edge,cv.RETR_TREE,cv.CHAIN_APPROX_SIMPLE)
    for i, contour in enumerate(contours):
        cv.drawContours(image,contours,i,(0,0,255),2)
    cv.imshow('contour',image)


src = cv.imread('smarties.png', 1)
cv.namedWindow('demo',cv.WINDOW_AUTOSIZE)
cv.imshow('demo', src)
find_contours_demo(src)
cv.waitKey(0)
cv.destroyWindow('demo')